﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Database;
public partial class admin_SuaMT : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var pm = new checkLogin();
        pm.redirectToLoginPage(); 
        if (!Page.IsPostBack)
            {
                if (Session["tenkh"] == "Admin")
                {
                    Response.Redirect("Login.aspx");
                }
                else
                {
                   //DDLLoaiMayTinh.DataSource = LoaiMayTinh.DSMaLoaiMT().Tables[0];
                   DDLLoaiMayTinh.DataSource = LoaiMayTinh.DSMaLoaiMT().Tables[0];
                   DDLLoaiMayTinh.DataTextField = "tenloai";
                  DDLLoaiMayTinh.DataValueField = "maloai";
                    DDLLoaiMayTinh.DataBind();

                  // ddlMaHangSX.DataSource = HangSX.DSMaHSX().Tables[0];
                   ddlMaHangSX.DataSource = HangSX.DSMaHSX().Tables[0];
                    ddlMaHangSX.DataTextField = "tenhang";
                    ddlMaHangSX.DataValueField = "mahangsx";
                  ddlMaHangSX.DataBind();
                    LayThongTin();
                }
            }
                     
        }
        public void LayThongTin()
        {
            if (Request.QueryString["mamay"] != null)
            {
                int mamay = int.Parse(Request.QueryString["mamay"].ToString());
                MayTinh mt = new MayTinh();
                mt = mt.ThongTinMT(mamay);
                this.txtMaMay.Text = mt.Mamay.ToString();
                this.txtTenMay.Text = mt.Tenmay;
                this.txtGiaBan.Text = mt.Giaban.ToString();
                this.txtSoLuong.Text = mt.Soluong.ToString();
                this.txtMoTa.Text = mt.Mota;
            }
            else
            {
            }
        }

        protected void btSave_Click1(object sender, EventArgs e)
        {
            int mamay = int.Parse(txtMaMay.Text);
            float Giaban =float.Parse(txtGiaBan.Text);
            int soluong = int.Parse(txtSoLuong.Text);            
            int mahangsx = int.Parse(ddlMaHangSX.SelectedValue.ToString());            
            int loaimt = int.Parse(DDLLoaiMayTinh.SelectedValue.ToString());
            string hinhanh;
            if (!String.IsNullOrEmpty(UpLoadHinhAnh.FileName))
            {
                String path = "~/images/" + UpLoadHinhAnh.FileName;
                UpLoadHinhAnh.SaveAs(MapPath(path));
                hinhanh = UpLoadHinhAnh.FileName.ToString();
            }
            else
            {
                hinhanh = "no_image.jpg";
            }
            bool hienthi;
            if (rbCo.Checked == true)
            {                
                hienthi = true;
            }
            else
            {                
                hienthi = false;
            }
            MayTinh mt = new MayTinh();
            mt.CapNhatMT(mamay,this.txtTenMay.Text, Giaban, soluong, mahangsx, this.txtMoTa.Text, loaimt, hinhanh, hienthi);
            Response.Redirect("DanhsachMT.aspx");// Trang hiển thị sản phẩm
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            txtTenMay.Text = "";
            txtGiaBan.Text = "";
            txtSoLuong.Text = "";
            txtMoTa.Text = "";            
        }
    
}
